package com.example.park

import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import com.example.base.entity.ParamNews
import com.example.base.mvvm.BaseActivity
import com.example.park.databinding.ActivityUpdateBinding

class UpdateActivity : BaseActivity<ActivityUpdateBinding,MainViewModel>() {
    lateinit var paramNews: ParamNews
    override fun initView() {
        val newsId :Int= intent?.getIntExtra("id",0)!!
        viewModel.detail(newsId)
        binding.update.setOnClickListener {
            paramNews.title  = binding.title.text.toString()
            paramNews.content = binding.content.text.toString()
            viewModel.update(newsId, paramNews)
        }
    }

    override fun initData() {
        viewModel.detailSuccess.observe(this){
            if(it.code==200){
                binding.detail = it.data
                paramNews = ParamNews(it.data.approved,it.data.content,it.data.id,it.data.images,it.data.publishDate,it.data.published,it.data.publisher,it.data.title,it.data.type)
            }
        }
        viewModel.updateSuccess.observe(this){
            if(it.code==200){
                finish()
                startActivity(Intent(this,MainActivity::class.java))
            }
        }
    }

    override fun getViewModel(): Class<MainViewModel> = MainViewModel::class.java

    override fun getLayout(): Int  = R.layout.activity_update


}